package fr.paris5.miniWars.Screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Scaling;

import fr.paris5.miniWars.MiniWars;


public abstract class AbstractScreen implements Screen
{
	// the fixed viewport dimensions (ratio: 1.6)
	public static final int DEFAULT_SCREEN_WITDH = 800, DEFAULT_SCREEN_HEIGHT = 480;

	protected final MiniWars game;
	protected final Stage stage;

	private SpriteBatch batch;

	public AbstractScreen(MiniWars game) {
		this.game = game;
		int width = DEFAULT_SCREEN_WITDH;
		int height = DEFAULT_SCREEN_HEIGHT;
		this.batch = new SpriteBatch();
		this.stage = new Stage( width, height, true, batch );
	}

	public SpriteBatch getBatch()
	{
		return batch;
	}

	// Screen implementation

	@Override
	public void show()
	{
		Gdx.input.setInputProcessor( stage );
	}

	@Override
	public void resize (int width, int height) {
	    Vector2 size = Scaling.fit.apply(800, 480, width, height);
	    int viewportX = (int)(width - size.x) / 2;
	    int viewportY = (int)(height - size.y) / 2;
	    int viewportWidth = (int)size.x;
	    int viewportHeight = (int)size.y;
	    Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
	    stage.setViewport(800, 480, true, viewportX, viewportY, viewportWidth, viewportHeight);
	}

	@Override
	public void render(float delta) {
		// Evolution des acteurs
		stage.act(delta);

		// Rendu graphique
		Gdx.gl.glClearColor( 0f, 0f, 0f, 1f );
		Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
		stage.draw();
	}

	@Override
	public void hide() {
		dispose();
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		batch.dispose();
		stage.dispose();
	}
}
